package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bean.User;
import com.db.UserDao;


@WebServlet(name = "UserServlet")
public class UserServlet extends HttpServlet{
	private void printContent(PrintWriter out) {
    }
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
		 resp.setContentType("text/html;charset=\"utf-8\"");
	     PrintWriter out = resp.getWriter();
	     String method = req.getParameter("method");
		
	   //处理登录界面
	        if (method.equals("login")) {
	            try {
	            	//获取从登录界面传递过来的参数值
	                String email = req.getParameter("email");
	                String password = req.getParameter("password");

	                printContent(out);
	                int result;

	                UserDao connect = new UserDao();
	                result = connect.login(email, password);

	                if (result == 0) {
	                    User u = new User();
	                    u = connect.queryID(email);
	                    System.out.println(u.getUsername());
	                    
	                    u.setEmail(email);
	                    u.setPassword(password);
	                    req.getSession().setAttribute("user", u);
	                    resp.sendRedirect("index.jsp");
	                }
	                if (result == 1) {
	                    resp.getWriter().print("<script> alert('密码错误！');location.href=\"login.jsp\"; </script>");
	                }
	                if (result == 2) {
	                    resp.getWriter().print("<script> alert('不存在该用户名!');location.href=\"login.jsp\";</script>");
	                }
	            } catch (IllegalAccessException e1) {
	                e1.printStackTrace();
	            } catch (InstantiationException e1) {
	                e1.printStackTrace();
	            } catch (SQLException e1) {
	                e1.printStackTrace();
	            } catch (ClassNotFoundException e1) {
	                e1.printStackTrace();
	            } catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	        }
	        

	      //处理注册界面
	        if (method.equals("register")) {

	            try {
	            	//获取从登录界面传递过来的参数ֵ
	                String email = new String(req.getParameter("email").getBytes("iso8859-1"),"UTF-8");
	                String password = new String(req.getParameter("password").getBytes("iso8859-1"),"UTF-8");
	                String password_check = new String(req.getParameter("password_check").getBytes("iso8859-1"),"UTF-8");
	                String name = new String(req.getParameter("name").getBytes("iso8859-1"),"UTF-8");

	                if(password.equals(password_check)==false)
	                {
	                    resp.getWriter().print("<script> alert('两次密码输入有误！');location.href=\"create.jsp\"; </script>");
	                }
	                UserDao connect = new UserDao();

	                int result = connect.register(email,name,password);

	                if (result == 0) 
	                {
	                    User newUser = new User();
	                    newUser.setEmail(email);
	                    newUser.setPassword(password);
	                    newUser.setName(name);
	                    req.getSession().setAttribute("newUser",newUser);
	                    resp.getWriter().print("<script> alert('注册成功！');location.href=\"login.jsp\"; </script>");
	                }
	                if (result == 1) {
	                    resp.getWriter().print("<script> alert('用户邮箱已存在！');location.href=\"create.jsp\"; </script>");
	                }
	                if (result == 2) {
	                    resp.getWriter().print("<script> alert('用户名已存在');location.href=\"create.jsp\"; </script>");
	                }
	                if (result == 3) {
	                    out.println("error");
	                }
	            } catch (ClassNotFoundException e) {
	                e.printStackTrace();
	            } catch (IllegalAccessException e) {
	                e.printStackTrace();
	            } catch (InstantiationException e) {
	                e.printStackTrace();
	            } catch (SQLException e) {
	                e.printStackTrace();
	            } catch (Exception e){
	            	e.printStackTrace();
	            }

	        }
	        
	        
	      //点击进入admin界面的操作
	        if(method.equals("admin"))
	        {
	            String ID = new String(req.getParameter("ID").getBytes("ISO-8859-1"),"UTF-8");
	            UserDao connct = new UserDao();
	            
	            try {
	            	User user = new User();
	                connct.searchUserInfo(ID,user);
	                req.getSession().setAttribute("userAdmin",user);
	                
	                resp.getWriter().print("<script>location.href=\"admin.jsp?ID=" + ID + "\"; </script>");
	            } catch (ClassNotFoundException e) {
	                e.printStackTrace();
	            } catch (IllegalAccessException e) {
	                e.printStackTrace();
	            } catch (InstantiationException e) {
	                e.printStackTrace();
	            } catch (SQLException e) {
	                e.printStackTrace();
	            } catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	        }
	        
	        
	        
	        
	        
	        
	        
	        
	        
	      //处理adminEdit界面，用于修改账户个人信息
	        if(method.equals("adminEdit"))
	        {
	        	String ID = req.getParameter("ID");
	            String email = req.getParameter("email");
	            String name = new String (req.getParameter("name").getBytes("ISO-8859-1"),"UTF-8");
	            String sex = new String (req.getParameter("sex").getBytes("ISO-8859-1"),"UTF-8");
	            int age = Integer.valueOf(req.getParameter("age")).intValue();
	            String introduction = new String (req.getParameter("introduction").getBytes("ISO-8859-1"),"UTF-8");



	            UserDao adminCustomer = new UserDao();
	            try {
	                adminCustomer.adminEdit(ID,email,name,sex,age,introduction);
	                resp.getWriter().print("<script>window.location.href='UserServlet?method=admin&ID=" + ID +"';</script>");

	            } catch (ClassNotFoundException e) {
	                e.printStackTrace();
	            } catch (IllegalAccessException e) {
	                e.printStackTrace();
	            } catch (InstantiationException e) {
	                e.printStackTrace();
	            } catch (SQLException e) {
	                e.printStackTrace();
	            } catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	        }
	        
	        
	        
	      //处理分享界面
	        if (method.equals("share")) 
	        {
	            try 
	            {

	                //获取从分享界面传递过来的参数值

	                String shareTitle = new String(req.getParameter("share_title").getBytes("ISO-8859-1"),"UTF-8");
	                String shareContent=new String(req.getParameter("share_content").getBytes("ISO-8859-1"),"UTF-8");
	                String shareID = new String(req.getParameter("ID").getBytes("ISO-8859-1"),"UTF-8");
	                String shareCity = new String(req.getParameter("share_city").getBytes("ISO-8859-1"),"UTF-8");
	                int shareCost = new Integer(req.getParameter("share_cost"));
	                int shareDays = new Integer(req.getParameter("share_days"));
	                String shareTime = new String(req.getParameter("share_time").getBytes("ISO-8859-1"),"UTF-8");
	                UserDao connect = new UserDao();



	                if(shareID.equals(""))
	                {
	                    resp.getWriter().print("<script> alert('请先登录！');location.href=\"login.jsp\"; </script>");
	                }

	                int result = connect.share(shareID,shareCity,shareTitle,shareContent,shareCost,shareDays,shareTime);
	                if (result == 0) 
	                {
	                	UserDao returnID = new  UserDao();
	                    int passageID = returnID.returnPassageID(shareTitle);
	                    resp.getWriter().print("<script> alert('分享成功！');location.href=\"travelDetails.jsp?passageID="+ passageID +"&page=1\";</script>");
	                }
	                if (result == 1) 
	                {
	                    resp.getWriter().print("<script> alert('error！'); location.href=\"index.jsp\";</script>");
	                }


	            } catch (ClassNotFoundException e) 
	            {
	                e.printStackTrace();
	            } catch (IllegalAccessException e) 
	            {
	                e.printStackTrace();
	            } catch (InstantiationException e)
	            {
	                e.printStackTrace();
	            } catch (SQLException e) 
	            {
	                e.printStackTrace();
	            } catch (Exception e) 
	            {
					e.printStackTrace();
				}

	        }
	        
	      //处理删除文章
	        if (method.equals("delete")) 
	        {
	        	try 
	            {

	                //获取从个人中心传递过来的参数值
	                int passageID = new Integer(req.getParameter("passageID"));
	                int ID =  new Integer(req.getParameter("ID"));
	                String email = new String(req.getParameter("email").getBytes("ISO-8859-1"),"UTF-8");
	                UserDao connect = new UserDao();

	                int result = connect.deletePassage(passageID);
	                if (result == 0) 
	                {
	                	resp.getWriter().print("<script>alert('删除成功！');window.location.href='UserServlet?method=admin&ID=" + ID +"';</script>");
	                }


	            } catch (ClassNotFoundException e) 
	            {
	                e.printStackTrace();
	            } catch (IllegalAccessException e) 
	            {
	                e.printStackTrace();
	            } catch (InstantiationException e)
	            {
	                e.printStackTrace();
	            } catch (SQLException e) 
	            {
	                e.printStackTrace();
	            } catch (Exception e) 
	            {
					e.printStackTrace();
				}
	        }
	}
	@Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        doPost(req, resp);
    }
}
